package com.lailai.bizobject;

import com.lailai.service.impl.AppConfiguration;
import com.lailai.metadata.BizObjectTable;
import com.lailai.metadata.Property;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author lgr
 * @Date 2025/9/15
 * @Description 业务对象工厂
 * @Email 983105834@qq.com
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BizObjectFactory {
    // 日志
    private final Logger logger = LoggerFactory.getLogger(BizObjectFactory.class);
    // 业务对象表名
    private String tableCode;
    // 业务对象字段集合
    private Map<String, Object> objectMap;
    // 配置
    private AppConfiguration appConfiguration;

    public static BizObject createBizObject(String schemaCode, Map<String, Object> objectMap, AppConfiguration appConfiguration) {
        return (new BizObjectFactory(schemaCode, objectMap, appConfiguration)).createBizObject();
    }

    public BizObject createBizObject() {
        BizObjectTable bizObjectTable = (BizObjectTable) objectMap.get("tableCode");
        Map<String, Property> propertyMap = (Map) bizObjectTable.getProperties().stream().collect(Collectors.toMap(Property::getId, Function.identity()));
        Map<String, BizObjectEntityProperty> bizObjectEntityPropertyMap = new HashMap(16);
        this.objectMap.forEach((key, value) -> {
            Property property = (Property) propertyMap.get(key);
//            if (property != null && (property.getDataType() != DataType.REFERENCE || !property.getExtensions().getCascaded())) {
//                bizObjectEntityPropertyMap.put(key, BizObjectEntityProperty.of(value, property));
//            }
        });
        BizObject bizObject = new BizObject();
        bizObject.setTableCode(tableCode);

        return bizObject;
    }
}
